package com.luych.toolbox.docker.service.sdk.container;

import com.luych.toolbox.docker.service.sdk.container.model.DockerContainerCreateParam;
import com.luych.toolbox.docker.service.sdk.container.model.DockerContainerCreateResult;
import com.luych.toolbox.docker.service.sdk.container.model.DockerContainersPruneResult;
import com.luych.toolbox.docker.service.sdk.container.model.DockerContainersResult;
import org.springframework.web.client.RestTemplate;

public class DockerContainers {

    private final String url;

    public DockerContainers(String url) {
        this.url = url;
    }

    public DockerContainer container(String id) {
        return new DockerContainer(url, id);
    }

    public DockerContainersResult all() {
        final String uri = url + "/containers/json?all={1}";
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(uri, DockerContainersResult.class, true);
    }

    public DockerContainersResult running() {
        final String uri = url + "/containers/json?all={1}";
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(uri, DockerContainersResult.class, false);
    }

    public DockerContainerCreateResult create(String name, DockerContainerCreateParam param) {
        final String uri = url + "/containers/create?name={1}";
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject(uri, param, DockerContainerCreateResult.class, name);
    }

    public DockerContainersPruneResult prune() {
        final String uri = url + "/containers/prune";
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject(uri, null, DockerContainersPruneResult.class);
    }

}
